package com.htbb.common.base;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.htbb.common.util.DateUtil;
import com.htbb.model.personalInsure.dto.Channel;
import com.htbb.order.entity.Product;
import com.htbb.order.entity.SysInterfaceUser;
import com.htbb.order.interfaces.SysInterfaceProdService;
import com.htbb.order.interfaces.SysInterfaceUserService;

/**
 * @date 2016-12-27
 * @description 配置文件解析类
 * @author wuyongde
 *
 */
public class MappingConfig {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public static String lastmodifydate = ""; //渠道最新更改记录时间
	public static String prod_lastmodifydate = ""; //产品最新更改记录时间
	
	/**
	 * 从数据库读取
	 */
	public static Map<String,Channel> db_channelConfig = new HashMap<String,Channel>();
	/**
	 * 从数据库读取
	 */
	public static Map<String,Product> db_productConfig = new HashMap<String,Product>();
	
	private static SysInterfaceUserService initInterfaceUserService;
	private static SysInterfaceProdService initInterfaceProdService;
	
	@Autowired
	private SysInterfaceUserService sysInterfaceUserService;
	@Autowired
	private SysInterfaceProdService sysInterfaceProdService;
	
	@PostConstruct
	public void init(){
		
		initInterfaceUserService = sysInterfaceUserService;
		initInterfaceProdService = sysInterfaceProdService;
		
		//初始化渠道信息
		initChannel();
	}
	
	/**
	 * 获取渠道信息
	 * @return
	 */
	public static void initChannel() {
		Channel channel = null;
		String lastmodifydate = initInterfaceUserService.getSysInterfaceUserLastmodifydate();
	    String prodlastmodifydate = initInterfaceProdService.getProductLastmodifydate();
		if(!MappingConfig.lastmodifydate.equals(lastmodifydate) || 
				!MappingConfig.prod_lastmodifydate.equals(prodlastmodifydate)){  //时间不一样代表修改了信息，重新获取
			List<SysInterfaceUser> list = initInterfaceUserService.selectAll();
			if(list != null && list.size() > 0){
				MappingConfig.lastmodifydate = DateUtil.dateToString(list.get(0).getModifyDate());
				List<Product> prodlist = initInterfaceProdService.getInterfaceProduct();
				for(SysInterfaceUser user : list){
				    channel = new Channel();
				    channel.setIsautopay(user.getIsautopay()+"");
				    channel.setIsPaylimit(user.getIspaylimit()+"");
				    channel.setIsResgister(user.getIsresgister()+"");
				    channel.setName(user.getName());
				    channel.setPartnerId(user.getPartnerId());
				    channel.setPartnerCode(user.getPartnercode());
				    channel.setPassword(user.getPassword());
				    channel.setUser(user.getUser());
				    if(user.getProdtypelist()==null ||"".equals(user.getProdtypelist())){
				    	channel.setProdTypeList(new LinkedList<String>());
				    }else{
				    	channel.setProdTypeList(Arrays.asList(user.getProdtypelist().split("\\|")));
				    }
				    
					if(prodlist != null && prodlist.size() > 0){
						MappingConfig.prod_lastmodifydate = prodlist.get(0).getModifyDate();
						Map<String,Product> productMap = new HashMap<String,Product>();
						for(Product product : prodlist){
							productMap.put(product.getProdType(), product);
						}
						MappingConfig.db_productConfig = productMap;
					}
				    
				    Map<String,Product> productMap = new HashMap<String,Product>();
				    if(channel.getProdTypeList().size() >0){
					    for(String prodType :channel.getProdTypeList()){
						    Map<String,Product> map = MappingConfig.db_productConfig;
						    Iterator<Map.Entry<String, Product>> entries = map.entrySet().iterator();  
						    while (entries.hasNext()) {  
						        Map.Entry<String, Product> entry = entries.next();  
						        if(prodType.equals(entry.getKey())){
						        	productMap.put(entry.getKey(), entry.getValue());
						        }
						    }
					    }
				    }
				    channel.setProductMap(productMap);
				    MappingConfig.db_channelConfig.put(user.getPartnercode(), channel);
				}
			}
		}
	}

	/**
	 * 获取渠道信息，包括两种方式，1从配置文件读取  2从数据库读取
	 * @param partnerCode
	 * @return
	 */
	public static Channel getChannel(String partnerCode) {
		//初始化渠道信息
		initChannel();
		return MappingConfig.db_channelConfig.get(partnerCode.toUpperCase());
	}

	

	
}
